package com.gonsin.gview.example.gview.group;

import com.gonsin.gview.annotations.GContext;
import com.gonsin.gview.annotations.GView;
import com.gonsin.gview.annotations.dynamic.GDynamicView;
import com.gonsin.gview.annotations.enums.ButtonAction;
import com.gonsin.gview.annotations.enums.ColumnType;
import com.gonsin.gview.example.data.GroupDataPicker;
import com.gonsin.gview.logic.config.GViewDynamicViewService;
import com.gonsin.gview.model.dynamic.DynamicButton;
import com.gonsin.gview.model.dynamic.DynamicProp;
import com.gonsin.gview.model.dynamic.data.DynamicTableData;
import com.gonsin.gview.model.dynamic.form.DynamicTextInput;
import com.gonsin.gview.model.dynamic.table.DynamicColumn;
import com.gonsin.gview.model.dynamic.table.DynamicHeader;
import com.gonsin.gview.model.dynamic.table.DynamicSearchBar;
import com.gonsin.gview.model.dynamic.table.DynamicTableView;
import com.gonsin.gview.model.struct.EnumItem;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

//@Component
//@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@GDynamicView(viewName = "GroupView")
public class GroupView implements DynamicTableView {

    @Override
    public String title(GContext context) {
        return "用户组";
    }

    @Override
    public DynamicTableData tableData(GContext context) {
        return DynamicTableData
                .fromTableDataPicker(GroupDataPicker.class)
                .keyName("groupId")
                .build();
    }

    @Override
    public DynamicHeader header(GContext context) {
        return DynamicHeader.builder()
                .buttons(Arrays.asList(
                        DynamicButton.builder()
                                .name("添加用户组")
                                .action(ButtonAction.VIEW_BY_DIALOG)
                                .view("AddGroupBean")
                        .build()))

                .searchBar(DynamicSearchBar.builder()
                        .filters(Arrays.asList(
                                DynamicTextInput.builder()
                                        .name("组名字")
                                        .keyName("name")
                                        .build()
                        ))
                        .build())
                .build();
    }

    @Override
    public List<DynamicColumn> columns(GContext context) {
        return Arrays.asList(
                DynamicColumn.builder()
                        .name("用户组名字")
                        .keyName("name")
                        .columnType(ColumnType.TEXT)
                        .build(),

                DynamicColumn.builder()
                        .name("审核权")
                        .keyName("audit")
                        .columnType(ColumnType.ENUMS)
                        .enums(Arrays.asList(
                                EnumItem.builder().value("true").title("是").build(),
                                EnumItem.builder().value("false").title("否").build()
                        ))
                        .build(),

                DynamicColumn.builder()
                        .name("免审核权")
                        .keyName("freeAudit")
                        .columnType(ColumnType.ENUMS)
                        .enums(Arrays.asList(
                                EnumItem.builder().value("true").title("是").build(),
                                EnumItem.builder().value("false").title("否").build()
                        ))
                        .build(),

                DynamicColumn.builder()
                        .name("免审核权")
                        .keyName("freeAudit")
                        .columnType(ColumnType.ENUMS)
                        .enums(Arrays.asList(
                                EnumItem.builder().value("true").title("是").build(),
                                EnumItem.builder().value("false").title("否").build()
                        ))
                        .build(),

                DynamicColumn.builder()
                        .name("管理员")
                        .keyName("admin")
                        .columnType(ColumnType.ENUMS)
                        .enums(Arrays.asList(
                                EnumItem.builder().value("true").title("是").build(),
                                EnumItem.builder().value("false").title("否").build()
                        ))
                        .build(),

                DynamicColumn.builder()
                        .name("后勤")
                        .keyName("waiter")
                        .columnType(ColumnType.ENUMS)
                        .enums(Arrays.asList(
                                EnumItem.builder().value("true").title("是").build(),
                                EnumItem.builder().value("false").title("否").build()
                        ))
                        .build(),

                DynamicColumn.builder()
                        .name("操作")
                        .columnType(ColumnType.BUTTONS)
                        .buttons(Arrays.asList(
                                DynamicButton.builder()
                                        .name("修改")
                                        .action(ButtonAction.VIEW_BY_DIALOG)
                                        .view("UpdateGroupBean")
                                        .props(
                                                Arrays.asList(
                                                        DynamicProp.builder()
                                                                .keyName("groupId")
                                                                .valueName("groupId")
                                                                .build()
                                                ))
                                        .build(),


                                DynamicButton.builder()
                                        .action(ButtonAction.CONFIRM_AND_CLOSE)
                                        .name("删除")
                                        .actionName("deleteGroup")
                                        .props(
                                                Arrays.asList(
                                                        DynamicProp.builder()
                                                                .keyName("groupId")
                                                                .valueName("groupId")
                                                                .build()
                                                ))
                                        .build()
                        ))
                        .build()
        );
    }
}
